package com.bill.app.serveur.bean.dao.implement;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.dialect.FirebirdDialect;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.bill.security.token.UsernamePasswordToken;

import com.bill.app.serveur.bean.Abonnes;
import com.bill.app.serveur.bean.Amis;
import com.bill.app.serveur.bean.dao.interfaces.MainDaoInterface;

public class MainDaoImplement implements MainDaoInterface{
private HibernateTemplate template;
public void setSessionFactory(SessionFactory sessionfactory){
	template=new HibernateTemplate(sessionfactory);
}
public  List<Object>  exec(String HQL,Object ...param){
	return template.find(HQL,param);
}
public Object  execForOne(String HQL,Object ...param){
	List liste = template.find(HQL,param);
	if(null==liste)return null;
	if(liste.size()<=0)return null;
	return liste.get(0);
	
}
@Override
public Abonnes loadAbonneByLogin(String login) {
	// TODO Auto-generated method stub
	
	 return (Abonnes) execForOne("from Abonnes where login=?", login);
}
@Override
public List<Abonnes> AbonneAll() {
	// TODO Auto-generated method stub
	    ArrayList<Abonnes>   l=(ArrayList) exec("from Abonnes");
		return l;
}
@Override
public List<Amis> loadAmis(String login) {
	// TODO Auto-generated method stub
	 @SuppressWarnings("unchecked")
	ArrayList<Amis>   l=(ArrayList) exec(""
	 		+ "select  new Amis(a.id, ab1, ab2)"
	 		+ " from Abonnes ab1,Abonnes ab2 , Amis a where "
	 		+ " ab1.login=a.abonne.login "
	 		+ " and "
	 		+ " ab2.login=a.amis.login "
	 		+ " and ab1.login=? ",login);
		return l;
}
@Override
public List<Amis> AllAmis() {
	// TODO Auto-generated method stub
	@SuppressWarnings("unchecked")
	ArrayList<Amis>   l=(ArrayList) exec(""
	 		+ "select  new Amis(a.id,  ab1, ab2)"
	 		+ " from Abonnes ab1,Abonnes ab2 , Amis a where "
	 		+ " ab1.login=a.abonne.login "
	 		+ " and "
	 		+ " ab2.login=a.amis.login "
	 		+ "   " );
		return l;
}
@Override
public boolean hasFriend(String ab, String friendlogin) {
	// TODO Auto-generated method stub
	 @SuppressWarnings("unchecked")
		ArrayList<Amis>   l=(ArrayList) exec(""
		 		+ "select  new Amis(a.id,  ab1, ab2)"
		 		+ " from Abonnes ab1,Abonnes ab2 , Amis a where "
		 		+ " ab1.login=a.abonne.login "
		 		+ " and "
		 		+ " ab2.login=a.amis.login "
		 		+ " and ab1.login=? and ab2.login=? ",ab,friendlogin);
			return l.size()>0;
}
@Override
public boolean hasFriend(Abonnes ab, String friendlogin) {
	// TODO Auto-generated method stub
	return hasFriend(ab.getLogin(),friendlogin);
}
@Override
public Amis loadAmis(String login, String loginami) {
	// TODO Auto-generated method stub
	 @SuppressWarnings("unchecked")
		ArrayList<Amis>   l=(ArrayList) exec(""
		 		+ "select  new Amis(a.id,  ab1, ab2)"
		 		+ " from Abonnes ab1,Abonnes ab2 , Amis a where "
		 		+ " ab1.login=a.abonne.login "
		 		+ " and "
		 		+ " ab2.login=a.amis.login "
		 		+ " and ab1.login=? and ab2.login=? ",login,loginami);
			return ((null!=l&&l.size()>0)?l.get(0):null);
}
@Override
public boolean isSamePassword(Abonnes ab, String pass) {
	// TODO Auto-generated method stub
	return ab!=null&&((null==pass&&null==ab.getPass()) ||(ab.getPass()!=null&&pass!=null&&ab.getPass().compareTo(pass)==0));
}
@Override
public boolean isSamePassword(String ab, String pass) {
	// TODO Auto-generated method stub
	Abonnes Abonnes = loadAbonneByLogin(ab);
	return isSamePassword(Abonnes,pass);
}
@Override
public Abonnes doAuthentification(String login, String password) {
	// TODO Auto-generated method stub
	 Abonnes ab = loadAbonneByLogin(login);
	 if(!isSamePassword(ab, password)) return null;
	 return ab;
}
@Override
public Abonnes doAuthentification(UsernamePasswordToken token) {
	// TODO Auto-generated method stub
	 if(token==null)return null;
	 return doAuthentification(token.getLogin(), token.getPassword());
}
@Override
public boolean save(Amis obj) {
	// TODO Auto-generated method stub
	try{
		template.saveOrUpdate(obj.getAbonne());
		template.saveOrUpdate(obj.getAmis());
		template.save(obj);
		return true;
	}catch(Exception e){
	return true;
	}
}
@Override
public boolean save(Abonnes obj) {
	// TODO Auto-generated method stub
	try{
		template.save(obj);
		return true;
	}catch(Exception e){
	return true;
	}
}
@Override
public void updateLoginInAmiTable(String login, String newLogin) {
	// TODO Auto-generated method stub
	List<Amis> l = loadAmis(login);
	 Abonnes ab=loadAbonneByLogin(login);
	 ab.setLogin(newLogin);
	 template.update(ab);
	for (Amis amis : l) {
	 amis.setAbonne(ab);
	 template.update(amis);
	}
}
@Override
public boolean update(Amis obj) {
	// TODO Auto-generated method stub
	try{
		Abonnes ab1=obj.getAbonne(),ab2=obj.getAmis();
		
		template.saveOrUpdate(ab1);
		template.saveOrUpdate(ab2);
		template.update(obj);
		return true;
	}catch(Exception e){
	return true;
	}
}
@Override
public boolean update(Abonnes obj) {
	// TODO Auto-generated method stub
	try{
		 
		template.update(obj);
		return true;
	}catch(Exception e){
	return true;
	}
}
@Override
public boolean delete(Amis obj) {
	// TODO Auto-generated method stub
	try{
		 template.delete(obj);
		return true;
	}catch(Exception e){
	return true;
	}
}
@Override
public boolean delete(Abonnes obj) {
	// TODO Auto-generated method stub
	try{
		 template.delete(obj);
		return true;
	}catch(Exception e){
	return true;
	}
}
@Override
public void deleteFriendFor(Abonnes ab) {
	// TODO Auto-generated method stub
	template.deleteAll(loadAmis(ab.getLogin()));
	//template.deleteAll(exec("from, param))
}
@Override
public void deleteFriendFor(UsernamePasswordToken token) {
	// TODO Auto-generated method stub
	deleteFriendFor(token.getLogin());
}
@Override
public void deleteFriendFor(String login) {
	// TODO Auto-generated method stub
	deleteFriendFor(loadAbonneByLogin(login));
	
}
@Override
public boolean delete(String login) {
	// TODO Auto-generated method stub
	return delete(loadAbonneByLogin(login));
}
@Override
public boolean persist(Object obj) {
	// TODO Auto-generated method stub
	try{
	  template.save(obj);
	  return true;
	}catch(Exception e){
		return false;
	}
}
/**
 * @return the template
 */
public HibernateTemplate getTemplate() {
	return template;
}
}
